package com.tusuo.stds.ui.suggest;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.UploadResponse;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SuggestPresenter.class)
class SuggestPresenter extends MvpBasePresenter<SuggestView> {

    @Inject
    ApiHelper apiHelper;

    private Context mContext;
    private String mContent;
    private String mContact;

    private Handler mHandlerShowErrMsg;

    SuggestPresenter(Context context) {
        mContext = context;
        mHandlerShowErrMsg = new Handler();

        Injector.initSuggestPresenterComponent().inject(this);
    }

    void upLoadImage(ArrayList<String> imageUrl,String content,String contact) {
        mContent = content;
        mContact = contact;

        ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setMessage("正在提交....");
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        if (imageUrl.size() != 0) {
            Map<String,RequestBody> maps = new HashMap<>();
            for (int i = 0; i < imageUrl.size(); i ++) {
                File file = new File(imageUrl.get(i));
                RequestBody request = RequestBody.create(MediaType.parse("multipart/form-data"),file);
                maps.put("file\"; filename=\"image"+ i + ".jpg\"",request);
            }

            apiHelper.uploadImg(new UploadImgSubscriber(dialog),"image.jpg",maps,"App_SuggestImage");
        }else {
            dialog.dismiss();
            apiHelper.commitSuggest(new CommitSuggestSubscriber(),content,"",contact);
        }

    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mHandlerShowErrMsg != null) {
            mHandlerShowErrMsg.removeCallbacksAndMessages(null);
        }

        super.detachView(retainInstance);
    }

    private Runnable showSloganRunnable = new Runnable() {
        @Override
        public void run() {
            if (getView() != null) {
                getView().showSlogan();
            }
        }
    };

    void showErrMsg(String text) {
        if (getView() != null) {
            getView().showErrMsg(text);
            mHandlerShowErrMsg.postDelayed(showSloganRunnable, 3000);
        }
    }

    private class UploadImgSubscriber extends Subscriber<UploadResponse> {
        ProgressDialog dialog;

        public UploadImgSubscriber(ProgressDialog dialog) {
            this.dialog = dialog;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(UploadResponse uploadResponse) {
            dialog.dismiss();
            if (uploadResponse != null && uploadResponse.Total > 0) {
                List<String> data = uploadResponse.Data;
                String imageUrl = "";

                for (int i = 0; i < data.size(); i ++) {
                    if (i == 0) {
                        imageUrl = imageUrl.concat(data.get(i));
                    }else {
                        imageUrl = imageUrl.concat(",").concat(data.get(i));
                    }
                }

                apiHelper.commitSuggest(new CommitSuggestSubscriber(), mContent,imageUrl, mContact);
            }

        }
    }

    private class CommitSuggestSubscriber extends Subscriber<ApiResult> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(ApiResult apiResult) {
            if (apiResult.checkState(mContext)) {
                Intent intent = new Intent(mContext,SuggestSuccessActivity.class);
                mContext.startActivity(intent);
                ((SuggestActivity_) mContext).finish();
            }else {
                Toast.makeText(mContext, "提交失败,请重试", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
